package com.jztd.servone.invoice.controller;

import cn.hutool.core.util.ObjectUtil;
import com.jztd.servone.invoice.dto.InvoiceTitleDTO;
import com.jztd.servone.invoice.param.InvoiceTitleParam;
import com.jztd.servone.invoice.service.InvoiceTitleService;
import com.jztd.platfrom.base.annotation.UnifiedResponseBody;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 发票抬头管理
 */
@UnifiedResponseBody
@RestController
@RequestMapping("/servone/invoice/title")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class InvoiceTitleController {
    private final InvoiceTitleService invoiceTitleService;

    /**
     * 创建发票抬头
     *
     * @param param 发票抬头参数对象，包含创建所需的信息
     * @return 创建成功的发票抬头DTO对象
     */
    @PostMapping("/create")
    public Boolean create(@Validated @RequestBody InvoiceTitleParam param) {
        param.validateParam();
        return invoiceTitleService.createInvoiceTitle(param);
    }

    /**
     * 更新发票抬头
     *
     * @param param 发票抬头参数对象，包含更新所需的信息
     * @return 更新成功的发票抬头DTO对象
     */
    @PutMapping("/update")
    public Boolean update(@Validated @RequestBody InvoiceTitleParam param) {
        if (ObjectUtil.isEmpty(param.getId())) {
            throw new IllegalArgumentException("ID不能为空");
        }
        param.validateParam();
        return invoiceTitleService.updateInvoiceTitle(param);
    }

    /**
     * 删除发票抬头
     *
     * @param id 发票抬头ID
     * @return 是否删除成功
     */
    @DeleteMapping("/delete/{id}")
    public Boolean delete(@PathVariable Long id) {
        return invoiceTitleService.deleteInvoiceTitle(id);
    }

    /**
     * 获取发票抬头列表
     *
     * @return 发票抬头DTO对象列表
     */
    @GetMapping("/list")
    public List<InvoiceTitleDTO> getList() {
        return invoiceTitleService.getInvoiceTitleList();
    }
}
